package com.felton.springbootflyway.service.impl;

import com.felton.springbootflyway.service.IAuxiliaryService;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author LiuRui
 * @version 1.0
 * @date 2019/12/10 17:14
 * @description
 */
@Service
public class AuxiliaryServiceImpl implements IAuxiliaryService {
    public static final String projectDir = "/home/liurui/shell";
    public static final String downloadTxtSh = "test1.sh";

    @Override
    public boolean checkUpdateStatus() {
        BufferedReader stdInput = null;
        BufferedReader stdError = null;
        ProcessBuilder processBuilder = new ProcessBuilder("./" + downloadTxtSh);
        processBuilder.directory(new File(projectDir));
        try {
            Process process = processBuilder.start();
            stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
            stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String s;
            while ((s = stdInput.readLine()) != null) {
                System.out.println("标准输出" + s);
            }
            while ((s = stdError.readLine()) != null) {
                System.out.println("错误输出" + s);
            }
            int runningStatus = process.waitFor();
            if (runningStatus != 0) {
                System.out.println("运行出错！！");
            }
            System.out.println("最后输出：" + s);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        {
            if (stdInput != null) {
                try {
                    stdInput.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (stdError != null) {
                try {
                    stdError.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        // 1. 请求获取文件
        // 2. 检查是否有新的文件需要更新
        // 3. 如果存在需要更新的文件 返回更新状态
        return false;
    }

    @Override
    public void upgrade() {
        // 项目重启
    }


}





